package chapter3.No3_1_等待和通知机制.j生产者_消费者模式实现.demo2;

/**
 * *：多生产者和多消费者：操作值-假死
 *    原因：因为有可能生产者唤醒了生产者。导致线程一直处于wait状态
 *    解决办法：将单个唤醒方法notify()修改成唤醒所有notifyAll()
 * Created by Administrator on 2016/11/19.
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        P p = new P(lock);
        C c = new C(lock);
        ThreadP[] threadPs = new ThreadP[2];
        ThreadC[] threadCs = new ThreadC[2];
        for (int i = 0; i < 2; i++) {
            threadPs[i] = new ThreadP(p);
            threadPs[i].setName("生产者" + i);
            threadCs[i] = new ThreadC(c);
            threadCs[i].setName("消费者" + i);
            threadPs[i].start();
            threadCs[i].start();
        }

        Thread.sleep(3000);

        int groupCount = Thread.currentThread().getThreadGroup().activeCount();
        System.out.println("groupCount:" + groupCount);

        Thread[] threadArray = new Thread[groupCount];
        Thread.currentThread().getThreadGroup().enumerate(threadArray);
        for(int i = 0; i < threadArray.length; i++) {
            System.out.println(threadArray[i].getName()+" "+threadArray[i].getState());
        }
    }
}
